import cv2 as cv
from common.config import config_option

opencv_haarcascades_file = "D:\\directory\\opencv\\sources\\data\\haarcascades\\haarcascade_frontalface_default.xml"

"""
图片人脸检测
"""


def face_detect_demo(src):
    # 图片转换灰度图片
    gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
    # 加载特征数据
    face_detector = cv.CascadeClassifier(opencv_haarcascades_file)
    #
    faces = face_detector.detectMultiScale(gray, scaleFactor=1.02, minNeighbors=5)
    print(faces)
    for x, y, w, h in faces:
        cv.rectangle(src, (x, y), (x + w, y + h), color=(0, 0, 255))
    cv.imshow('result', src)


src = cv.imread(config_option['project_path'] + "/data/face_detect/a1.jpg")
# cv.imshow('result', src)
face_detect_demo(src)
cv.waitKey(0)
cv.destroyAllWindows()
